package com.ltsw.mvpbaseproject.baseView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.ltsw.mvpbaseproject.basePresenter.BasePresenter;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author FangHeQiang
 * @description:
 * @date :2019/8/28 14:35
 */
public abstract class BaseFragment<V extends BaseActivity, T extends BasePresenter> extends Fragment implements BaseView {
    protected V mActivity;
    protected T mPresenter;
    private View inflate;
    private Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (inflate == null) {
            inflate = inflater.inflate(getLayoutId(), null);
            unbinder = ButterKnife.bind(this, inflate);
            mActivity = (V) getActivity();
            mPresenter = getPresenter();
            mPresenter.attachView(this, mActivity);
            initView();
            initData();
        }
        if (inflate.getParent() != null) {
            ((ViewGroup) inflate.getParent()).removeView(inflate);
        }
        return inflate;
    }

    /**
     * 初始化控件
     */
    public abstract void initView();

    /**
     * 为控件添加点击事件
     */
    public abstract void initData();

    /**
     * 实例化Presenter
     */
    public abstract T getPresenter();


    /**
     * 传递用户创建的Layout  ID值
     */
    protected abstract int getLayoutId();

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.detachView();
        unbinder.unbind();
    }
}
